﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoplanetaTaskManager.Models;

namespace AutoplanetaTaskManager.Daos.EntityFramework
{
    public class PostDao : IPostDao
    {
        public List<Post> Load()
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Post.ToList();
        }

        public Post Get(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
                return db.Post.FirstOrDefault(m => m.Id == id);
        }

        public void Add(Post post)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                db.AddToPost(post);
                db.SaveChanges();
            }
        }

        public void Update(Post post)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                Post p = db.Post.FirstOrDefault(m => m.Id == post.Id);
                p.Name = post.Name;
                db.SaveChanges();
            }
        }

        public void Remove(int id)
        {
            using (AtmDbEntities db = new AtmDbEntities())
            {
                Post post = db.Post.FirstOrDefault(m => m.Id == id);
                if (post != null) db.DeleteObject(post);
                db.SaveChanges();
            }
        }
    }
}
